﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ScormModel;
namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for InputData.xaml
    /// </summary>
    public partial class InputData : Window
    {
        TreeViewItem selectedItem;
        ImsManifest imsmanifest;
        string action;
        TreeView tvMain;
        public TreeView MainTree
        {
            get { return this.tvMain; }
            set { this.tvMain = value; }
        }
        public TreeViewItem SelectedItem
        {
            get { return this.selectedItem; }
            set { this.selectedItem = value; }
        }
        public ImsManifest Imsmanifest
        {
            get { return this.imsmanifest; }
            set { this.imsmanifest = value; }
        }
        public string Action
        {
            get { return this.action; }
            set { this.action = value; }
        }
        public InputData()
        {
            InitializeComponent();
            contentName.Focus();
        }
        private void ButtonContentCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        private void ButtonContentOK_Click(object sender, RoutedEventArgs e)
        {
            TreeViewProcessing tcp = new TreeViewProcessing();
            tcp.Imsmanifest = imsmanifest;
            tcp.MainTree = tvMain;
            ManifestProcessing mp = new ManifestProcessing(imsmanifest);
            SequenceProcessing sp = new SequenceProcessing();
            switch (imsmanifest.TemplateCourse)
            {
                #region Custom Template
                case "Custom":
                    if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }

                        
                        // for manifest                        
                        Item newItem = mp.initModuleItemForCustomTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        string type = mp.findTypeOfItem(selectItemName);
                        Boolean result = false;
                        if (type == "COURSE")
                        {
                            Organization parentItem = mp.findOrganizationByName(selectItemName);
                            result = mp.addItemForCustomTemplate(parentItem, newItem);
                        }
                        else if (type == "MODULE")
                        { 
                             Item parentItem = mp.findItemByName(selectItemName);
                             result = mp.addItemForCustomTemplate(parentItem, newItem);
                        }
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForCustomTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddTest")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        
                        // for manifest                        
                        Item newItem = mp.initTestItemForCustomTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        string type = mp.findTypeOfItem(selectItemName);
                        Boolean result = false;
                        if (type == "COURSE")
                        {
                            Organization parentItem = mp.findOrganizationByName(selectItemName);
                            result = mp.addItemForCustomTemplate(parentItem, newItem);
                        }
                        else if (type == "MODULE")
                        {
                            Item parentItem = mp.findItemByName(selectItemName);
                            result = mp.addItemForCustomTemplate(parentItem, newItem);
                        }
                        if (result == true)
                        {
                            //for GUI
                            tcp.addTestForCustomTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddExam")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        
                        // for manifest                        
                        Item newItem = mp.initExamItemForCustomTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        string type = mp.findTypeOfItem(selectItemName);
                        Boolean result = false;
                        if (type == "COURSE")
                        {
                            Organization parentItem = mp.findOrganizationByName(selectItemName);
                            result = mp.addItemForCustomTemplate(parentItem, newItem);
                        }
                        else if (type == "MODULE")
                        {
                            Item parentItem = mp.findItemByName(selectItemName);
                            result = mp.addItemForCustomTemplate(parentItem, newItem);
                        }
                        if (result == true)
                        {
                            //for GUI
                            tcp.addExamForCustomTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        
                        // for manifest                        
                        Item newItem = mp.initLessonItemForCustomTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        string type = mp.findTypeOfItem(selectItemName);
                        Boolean result = false;
                        if (type == "COURSE")
                        {
                            Organization parentItem = mp.findOrganizationByName(selectItemName);
                            result = mp.addItemForCustomTemplate(parentItem, newItem);
                        }
                        else if (type == "MODULE")
                        {
                            Item parentItem = mp.findItemByName(selectItemName);
                            result = mp.addItemForCustomTemplate(parentItem, newItem);
                        }
                        if (result == true)
                        {
                            //for GUI
                            tcp.addLessonForCustomTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        
                        Item newItem = mp.initQuestionItemForCustomTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForCustomTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForNoneTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameNodeForCustomTemplate(selectedItem, contentName))
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForCustomTemplate(selectedItem, contentName))
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForCustomTemplate(selectedItem) )
                        {
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            tcp.removeTreeViewItem(selectedItem);
                            Close();
                        }
                    }
                break;
                #endregion
                #region None Template
                case "None":
                    if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        // for manifest                        
                        Item newItem = mp.initModuleItemForNoneTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Organization parentItem = mp.findOrganizationByName(selectItemName);
                        Boolean result = mp.addModuleForNoneTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForNoneTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForNoneTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForNoneTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addLessonForNoneTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initQuestionItemForNoneTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForNoneTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForNoneTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if ( mp.renameNodeForNoneTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }

                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForNoneTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForNoneTemplate(selectedItem))
                        {
                            tcp.removeTreeViewItem(selectedItem);
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            Close();
                        }
                    }
                    break;
                #endregion
                #region Linear Template
                case "Linear":
                    if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        // for manifest                        
                        Item newItem = mp.initModuleItemForLinearTemplate(contentName);
                        newItem.Sequencing = sp.buildModuleSequencingForLinearTemplate();
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Organization parentItem = mp.findOrganizationByName(selectItemName);
                        Boolean result = mp.addModuleForLinearTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForLinearTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForLinearTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForLinearTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addLessonForLinearTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initQuestionItemForLinearTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForLinearTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForLinearTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameNodeForLinearTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }

                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForLinearTemplate(selectedItem, contentName))
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForLinearTemplate(selectedItem))
                        {
                            tcp.removeTreeViewItem(selectedItem);
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            Close();
                        }
                    }
                    break;
                #endregion
                #region Linear_Choice Template
                case "Linear_Choice":
                    if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initModuleItemForLinearChoiceTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForLinearChoiceTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForLinearChoiceTemlate(selectedItem, contentName);
                            // for manifest                        
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForLinearChoiceTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForLinearChoiceTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addLessonForLinearChoiceTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initQuestionItemForLinearChoiceTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForLinearChoiceTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForLinearChoiceTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameNodeForLinearChoiceTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }

                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForLinearChoiceTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForLinearChoiceTemplate(selectedItem) )
                        {
                            tcp.removeTreeViewItem(selectedItem);
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            Close();
                        }
                    }
                    break;
                #endregion
                #region Linear_Controls Template
                case "Linear_Controls":
                    if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        // for manifest                        
                        Item newItem = mp.initModuleItemForLinearControlTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Organization parentItem = mp.findOrganizationByName(selectItemName);
                        Boolean result = mp.addModuleForLinearControlTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForLinearControlTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForLinearControlTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForLinearControlTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addLessonForLinearControlTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initQuestionItemForLinearControlTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForLinearControlTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForLinearControlTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameNodeForLinearControlTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }

                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForLinearControlTemplate(selectedItem, contentName))
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForLinearControlTemplate(selectedItem))
                        {
                            tcp.removeTreeViewItem(selectedItem);
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            Close();
                        }
                    }
                    break;
                #endregion
                #region Constrained_Choice Template
                case "Constrained_Choice":
                     if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        // for manifest                        
                        Item newItem = mp.initModuleItemForConstrainedChoiceTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Organization parentItem = mp.findOrganizationByName(selectItemName);
                        Boolean result = mp.addModuleForConstrainedTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForConstrainedChoiceTemlate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForConstrainedChoiceTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForConstrainedChoiceTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addLessonForConstrainedChoiceTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initQuestionItemForConstrainedChoiceTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForConstrainedChoiceTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForConstrainedChoiceTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameNodeForConstrainedChoiceTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }

                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForConstrainedChoiceTemplate(selectedItem, contentName))
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForConstrainedChoiceTemplate(selectedItem))
                        {
                            tcp.removeTreeViewItem(selectedItem);
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            Close();
                        }
                    }
                    break;
                #endregion
                #region Competency Template
                case "Competency":
                    if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        // for manifest                        
                        Item newItem = mp.initModuleItemForCompetencyTemplate(contentName, imsmanifest.ManifestOrganizations.ListOrganization[0]);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Organization parentItem = mp.findOrganizationByName(selectItemName);
                        Boolean result = mp.addModuleCompetencyTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForCompetencyTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForKnowledgePacedTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForCompetencyTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addLessonForCompetencyTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initQuestionItemForCompetencyTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForCompetencyTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForCompetencyTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameNodeForCompetencyTemplate(selectedItem, contentName))
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }

                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForCompetencyTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForCompetencyTemplate(selectedItem) )
                        {
                            tcp.removeTreeViewItem(selectedItem);
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            Close();
                        }
                    }
                    break;
                #endregion
                #region KnowledgePaced Template
                case "KnowledgePaced":
                    if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        // for manifest                        
                        Item newItem = mp.initModuleItemForKnowledgePacedTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForKnowledgePacedTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForKnowledgePacedTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForKnowledgePacedTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForKnowledgePacedTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addLessonForKnowledgePacedTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initQuestionItemForKnowledgePacedTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForKnowledgePacedTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForKnowledgePacedTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameNodeForKnowledgePacedTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }

                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForKnowledgePacedTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForKnowledgePacedTemplate(selectedItem) )
                        {
                            tcp.removeTreeViewItem(selectedItem);
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            Close();
                        }
                    }
                    break;
                #endregion
                #region KnowledgePaced Template
                case "KnowledgePaced_Reuse":
                    if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        // for manifest                        
                        Item newItem = mp.initModuleItemForKnowledgePacedReusedTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForKnowledgePacedReusedTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForKnowledgePacedReusedTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForKnowledgePacedReusedTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForKnowledgePacedReusedTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addLessonForKnowledgePacedReusedTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initQuestionItemForKnowledgePacedReusedTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForKnowledgePacedReusedTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForKnowledgePacedReusedTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameNodeForKnowledgePacedReusedTemplate(selectedItem, contentName))
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }

                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForKnowledgePacedReusedTemplate(selectedItem, contentName))
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForKnowledgePacedReusedTemplate(selectedItem))
                        {
                            tcp.removeTreeViewItem(selectedItem);
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            Close();
                        }
                    }
                    break;
                #endregion
                #region Remediation Template
                case "Remediation":
                     if (action == "AddModule")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        // for manifest 
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Organization parentItem = mp.findOrganizationByName(selectItemName);
                        Item newItem = mp.initModuleItemForRemediationTemplate(contentName, parentItem);
                        Boolean result = mp.addModuleForRemediationTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addModuleForRemediationTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddLesson")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForRemediationTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForRemediationTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            //for GUI
                            tcp.addLessonForRemediationTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "AddQuestion")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        Item newItem = mp.initLessonItemForRemediationTemplate(contentName);
                        string selectItemName = mp.getNameOfTreeViewItem(selectedItem);
                        Item parentItem = mp.findItemByName(selectItemName);
                        Boolean result = mp.addItemForRemediationTemplate(parentItem, newItem);
                        if (result == true)
                        {
                            tcp.addQuestionForRemediationTemplate(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Rename")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameNodeForRemediationTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }

                    }
                    else if (action == "RenameCourse")
                    {
                        if (imsmanifest.ListTitleHistory[contentName.Text] != null)
                        {
                            errorLog.Content = "Item with name " + contentName.Text + " already exist";
                            return;
                        }
                        else
                        {
                            imsmanifest.ListTitleHistory.Add(contentName.Text, contentName.Text);
                        }
                        if (mp.renameCourseForRemediationTemplate(selectedItem, contentName) )
                        {
                            tcp.renameTreeViewItem(selectedItem, contentName);
                            Close();
                        }
                    }
                    else if (action == "Remove")
                    {
                        if (mp.removeItemForRemediationTemplate(selectedItem))
                        {
                            tcp.removeItemForRemediationTemplate(selectedItem);
                            imsmanifest.ListTitleHistory.Remove(mp.getNameOfTreeViewItem(selectedItem));
                            Close();
                        }
                    }
                    break;
                #endregion
                default:
                    break;
            }

        }
    }
}
